package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.Grade;
import com.service.GradeService;
import com.service.impl.GradeServiceImpl;

/**
 * Servlet implementation class UpdateGradeServlet
 */
@WebServlet("/UpdateGradeServlet")
public class UpdateGradeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateGradeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置响应编码
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		String action = request.getParameter("action");
		if (action.equals("useGrade")) {
			int gid = Integer.parseInt(request.getParameter("gid"));
			GradeService gradeService = new GradeServiceImpl();
			request.setAttribute("grade", gradeService.selectByPrimaryKey(gid));
			request.getRequestDispatcher("UpdateGrade.jsp").forward(request, response);
		}
		if (action.equals("updateGrade")) {
			int gid = Integer.parseInt(request.getParameter("gid"));
			String gname = request.getParameter("gname");
			Grade grade = new Grade();
			grade.setGid(gid);
			grade.setGname(gname);
			GradeService gs = new GradeServiceImpl();
			int temp = gs.updateByPrimaryKey(grade);
			if (temp > 0) {
				out.print("<script>alert('修改成功');location.href='SelectAllGradeServlet'</script>");
				// response.sendRedirect("SelectAllGradeServlet");
			} else {
				out.print("<script>alert('修改失败');location.href='SelectAllGradeServlet'</script>");
				request.getRequestDispatcher("SelectAllGradeServlet").forward(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
